interface IFnMap  {
  [key: string]: Function[];
};

class EventEmit {
  private fnMap: IFnMap = {}

  public on(type: string, fn: Function) {
    if (this.fnMap[type]) {
      this.fnMap[type].push(fn);
      return;
    }
    this.fnMap[type] = [fn];
  }

  public trigger(type: string, data: any) {
    if (!this.fnMap[type]) {
      throw new Error(`${type} is not listen`);
    }
    this.fnMap[type].forEach((fn) => {
      if (typeof fn === 'function') {
        fn(data)
      }
    })
  }

  public remove (type: string) {
    this.fnMap[type] = [];
  }

  public removeAll () {
    this.fnMap = {}
  }
}


export default EventEmit;
